/** FASSIT (Forms of Selective Attention in Intelligent Transportation Systems)
 * Computational Creativity and Digital Media
 * Cognitive and Media Systems Group
 * Centre for Informatics and Systems of the University of Coimbra (CISUC)
 *
 * Copyright (c) 2010-2013 University of Coimbra, Portugal
 * All rights reserved.
 */
package agent.Simulation;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import poi.Info.Location;
import agent.Context.Context;

/**
 * This class is used to read from file the contexts used in the training and
 * test process
 * 
 * @author Hernani Costa
 * @version 0.1 date: 9/12/2012
 */
public class SimulationCoordenates {
	private static final String TRAINING_DIR = "DB/locations/training.txt";
	private static final String TEST_DIR = "DB/locations/test.txt";

	private ArrayList<Context> contextsForTraining = null,
			contextsForTesting = null;

	/**
	 * ------------------------------------------------------------------------
	 * Constructor class
	 * ------------------------------------------------------------------------
	 * 
	 * @param radius
	 *            -radius value that is used to create the context object
	 */
	public SimulationCoordenates(int radius) {
		contextsForTraining = new ArrayList<Context>();
		contextsForTesting = new ArrayList<Context>();
		read(radius);
	}

	/**
	 * ------------------------------------------------------------------------
	 * This function reads from file the contexts used in the training and test
	 * process
	 * ------------------------------------------------------------------------
	 * 
	 * @param radius
	 *            -radius value that is used to create the context object
	 */
	private void read(int radius) {
		try {
			BufferedReader train = new BufferedReader(new FileReader(
					TRAINING_DIR));
			BufferedReader test = new BufferedReader(new FileReader(
					TEST_DIR));
			contextsForTraining = readContextsFromFile(train, radius);
			contextsForTesting = readContextsFromFile(test, radius);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

	}

	/**
	 * ------------------------------------------------------------------------
	 * Low level functions. This function reads from file the contexts used in
	 * the training and test process
	 * ------------------------------------------------------------------------
	 * 
	 * @param file
	 *            - BufferedReader object
	 * @param radius
	 *            - radius value that is used to create the context object
	 * @return an ArrayList of Contexts
	 */
	private ArrayList<Context> readContextsFromFile(BufferedReader file,
			int radius) {
		String line = "", timeOfDay = "", dayOfWeek = "", goal = "";
		String[] str;
		double lat = 0.0, lng = 0.0;
		int runID = 0;

		ArrayList<Context> contexts = new ArrayList<Context>();
		Context contextTemp = null;
		Location userLocation = null;

		try {
			while ((line = file.readLine()) != null) {
				// The line contains:
				// runID, lat,lng,timeOfDay,dayOfWeek,goal
				str = line.split(",");
				runID = Integer.parseInt(str[0]);
				lat = Double.parseDouble(str[1].trim());
				lng = Double.parseDouble(str[2].trim());
				userLocation = new Location(lat, lng);

				timeOfDay = str[3];
				dayOfWeek = str[4];
				goal = str[5];

				contextTemp = new Context(userLocation, timeOfDay, dayOfWeek,
						goal, radius, runID);
				contexts.add(contextTemp);
			}// END While

			file.close();
			return contexts;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}

	}// END readContextsFromFile

	// ------------------------------------------------------------------------
	// ------------------------GETs and SETs-----------------------------------
	// ------------------------------------------------------------------------

	public ArrayList<Context> getContextsForTraining() {
		return contextsForTraining;
	}

	public ArrayList<Context> getContextsForTesting() {
		return contextsForTesting;
	}

//	/**
//	 * ------------------------------------------------------------------------
//	 * Used to provide an example that exercises the functionality of the class.
//	 * ------------------------------------------------------------------------
//	 * 
//	 * @param args
//	 */
//	public static void main(String args[]) {
//		SimulationCoordenates sc = new SimulationCoordenates(200);
//		 ArrayList<Context> ctraining = sc.getContextsForTraining();
//		 ArrayList<Context> ctest = sc.getContextsForTesting();
//		 
//		 System.out.println("training:");
//		 for (Context c : ctraining){
//			 System.out.println(c.toString());
//		 }
//		 
//		 System.out.println("test:");
//		 for (Context c : ctest){
//			 System.out.println(c.toString());
//		 }
//		 
//	}
	
}
